﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TK3DSim;
using Infragistics.Win.UltraWinTree;
using TK3DSim.Mil;
using System.Collections;
using Infragistics.Win;
//using FormMIL;
using System.Text.RegularExpressions;
namespace Tpro6Container
{
    public partial class FrmShowKinds : Form
    {
        private ArmKinds p_armkinds;
        private bool modifyed;
        public FrmShowKinds()
        {
            p_armkinds = Global.Instance.GlobalArmKinds;
            InitializeComponent();
            modifyed = false;

        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.InitialDirectory = Application.StartupPath + @"\img\";
                ofd.RestoreDirectory = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {


                    foreach (string file in ofd.FileNames)
                    {

                        if (file.Trim().Substring(file.Length - 3).CompareTo(".db") == 0)
                        {
                            continue;
                        }

                        string odl = Application.StartupPath + @"\img\";
                        string newfile = file.Remove(0, odl.Length);

                        if (!skipCoCombox(comKind, newfile))
                        {
                            modifyed = true;
                            comKind.Items.Add(newfile).Tag = "0,0,0,0,0.010,";
                        }
                    }
                    comKind.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {

                comKind.SelectedIndex = 0;
            }
        }

        private bool skipCoCombox(Infragistics.Win.UltraWinEditors.UltraComboEditor comKind, string file)
        {
            for (int i = 0; i < comKind.Items.Count; i++)
            {
                if (file.Trim().CompareTo(comKind.Items[i].DisplayText.Trim()) == 0)
                {
                    return true;
                }

            }
            return false;
        }

        private void ultraButton4_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();

                ofd.RestoreDirectory = true;

                ofd.InitialDirectory = Application.StartupPath + @"\Model\";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    modifyed = true;
                    if (ofd.FileName.Trim().Substring(ofd.FileName.Length - 3).CompareTo(".db") == 0)
                    {
                        return;
                    }
                    this.ultraTextEditor1.Text = ofd.FileName.Remove(0, ofd.InitialDirectory.Length + 1);
                }
            }
            catch
                (Exception ex)
            {
            }

        }

        private void ultraButton2_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();

                ofd.RestoreDirectory = true;
                ofd.InitialDirectory = Application.StartupPath + @"\Model\";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (ofd.FileName.Trim().Substring(ofd.FileName.Length - 3).CompareTo(".db") == 0)
                    {
                        return;
                    }
                    modifyed = true;
                    this.ultraTextEditor2.Text = ofd.FileName.Remove(0, ofd.InitialDirectory.Length + 1);
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void ultraButton3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();

                ofd.RestoreDirectory = true;
                ofd.InitialDirectory = Application.StartupPath + @"\Model\";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (ofd.FileName.Trim().Substring(ofd.FileName.Length - 3).CompareTo(".db") == 0)
                    {
                        return;
                    }
                    modifyed = true;
                    this.ultraTextEditor3.Text = ofd.FileName.Remove(0, ofd.InitialDirectory.Length + 1);
                }
            }
            catch (Exception ex)
            {
            }

        }

        private void ultraButton5_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.RestoreDirectory = true;
                ofd.InitialDirectory = Application.StartupPath + @"\img\";
                if (ofd.ShowDialog() == DialogResult.OK)
                {

                    foreach (string file in ofd.FileNames)
                    {
                        if (file.Trim().Substring(file.Length - 3).CompareTo(".db") == 0)
                        {
                            continue;
                        }
                        string odl = Application.StartupPath + @"\img";
                        string newfile = file.Remove(0, odl.Length + 1);
                        if (!skipCoCombox(ultraComboEditor1, newfile))
                        {
                            modifyed = true;
                            this.ultraComboEditor1.Items.Add(newfile).Tag = "1,1,1,1,0.01,";

                        }

                    }
                    this.ultraComboEditor1.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void ultraButton6_Click(object sender, EventArgs e)
        {

            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = true;
                ofd.RestoreDirectory = true;
                ofd.InitialDirectory = Application.StartupPath + @"\img";
                if (ofd.ShowDialog() == DialogResult.OK)
                {

                    foreach (string file in ofd.FileNames)
                    {
                        if (file.Trim().Substring(file.Length - 3).CompareTo(".db") == 0)
                        {
                            continue;
                        }
                        string odl = Application.StartupPath + @"\img\";
                        string newfile = file.Remove(0, odl.Length + 1);
                        if (!skipCoCombox(ultraComboEditor2, newfile))
                        {
                            modifyed = true;
                            this.ultraComboEditor2.Items.Add(newfile).Tag = "1,"+System.IO.Path.GetFileNameWithoutExtension(newfile);
                        }

                    }
                    this.ultraComboEditor2.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void bSave_Click(object sender, EventArgs e)
        {

            if (this.treeArmy.SelectedNodes.Count <= 0)
            {
                return;
            }
            modifyed = false;
            string nodeuuid = treeArmy.SelectedNodes[0].Key;

            ArmKinds db = Global.Instance.GlobalArmKinds;
            //图片特效
            db.DelteWeapon(nodeuuid);
            for (int i = 0; i < comKind.Items.Count; i++)
            {
                string tag = (string)comKind.Items[i].Tag;
                string[] ar = Regex.Split(tag, ",");

                db.Add( ShowWhat.自身, nodeuuid, OwnerKinds.图片, comKind.Items[i].DisplayText, ar[0],ar[1],ar[2],ar[3],ar[4],ar[5]);
            }
            if (ultraTextEditor1.Text.Length != 0)
            {
                db.Add( ShowWhat.自身, nodeuuid, OwnerKinds.进攻行进, ultraTextEditor1.Text, "1", "0", "0", "0", "0","");
                

            }
            if (ultraTextEditor2.Text.Length != 0)
            {
                db.Add( ShowWhat.自身, nodeuuid, OwnerKinds.毁伤, ultraTextEditor2.Text, "1", "0", "0", "0", "0", "");

            }
            if (ultraTextEditor3.Text.Length != 0)
            {
                db.Add( ShowWhat.自身, nodeuuid, OwnerKinds.后退, ultraTextEditor3.Text, "1", "0", "0", "0", "0", "");

            }
            for (int i = 0; i < ultraComboEditor1.Items.Count; i++)
            {
                string tag = (string)ultraComboEditor1.Items[i].Tag;
                string[] ar = Regex.Split(tag, ",");
                db.Add( ShowWhat.目标, nodeuuid, OwnerKinds.图片, ultraComboEditor1.Items[i].DisplayText, ar[0], ar[1], ar[2], ar[3], "0",ar[5]);
            }
            OwnerKinds targetType = OwnerKinds.图片单向线型;

            for (int i = 0; i < ultraComboEditor2.Items.Count; i++)
            {
                string  tags = ultraComboEditor2.Items[i].Tag.ToString();
              string  [] array  =   Regex.Split(tags, ",");

                db.Add( ShowWhat.目标, nodeuuid, targetType, ultraComboEditor2.Items[i].DisplayText,"0", "0", "0", array[0], "0",array[1]);
            }
            MessageBox.Show("保存完毕！");

        }




        private void FrmShowKinds_Load(object sender, EventArgs e)
        {
            Armament oAt = null;

            for (int i = 0; i < Global.Instance.GlobalArmaments.Count; i++)
            {
                oAt = Global.Instance.GlobalArmaments[i];
                treeArmy.Nodes.Add(oAt.WeaponID, oAt.Name);
            }

            if (treeArmy.Nodes.Count > 0)
            {

                treeArmy.Nodes[0].Selected = true;
            }
            else
            {

                ClearForm();
                DisableForm();
            }

        }

        private void DisableForm()
        {



        }
        private void EnableForm()
        {

            ultraButton1.Enabled = true;

            ultraButton2.Enabled = true;
            ultraButton3.Enabled = true;
            ultraButton4.Enabled = true;

            ultraButton5.Enabled = true;
            ultraButton6.Enabled = true;

            ultraNumericEditor1.Enabled = true;
            ultraNumericEditor2.Enabled = true;
            ultraNumericEditor3.Enabled = true;
            ultraNumericEditor4.Enabled = true;
            ultraNumericEditor5.Enabled = true;
            ultraNumericEditor6.Enabled = true;
            ultraNumericEditor7.Enabled = true;
            ultraNumericEditor8.Enabled = true;

            ultraNumericEditor1.Enabled = true;
            bSave.Enabled = true;
            button1.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;

        }
        private void treeArmy_AfterSelect(object sender, SelectEventArgs e)
        {
            if (treeArmy.SelectedNodes.Count <= 0)
            {
                return;
            }
            EnableForm();
            ClearForm();
            string weponuuid = treeArmy.SelectedNodes[0].Key;

            ArrayList items = Global.Instance.GlobalArmKinds.ListWeapon(weponuuid);

            for (int i = 0; i < items.Count; i++)
            {
                ArmKind item = (ArmKind)items[i];

                if (item.ShowWhat == (int)ShowWhat.自身)
                {
                    if (item.ShowKind == (int)OwnerKinds.图片)
                    {

                        comKind.Items.Add(item.Filename).Tag = item.PositionScale;
                    }
                    if (item.ShowKind == (int)OwnerKinds.后退)
                    {
                        ultraTextEditor3.Text = item.Filename;
                    }
                    if (item.ShowKind == (int)OwnerKinds.毁伤)
                    {
                        ultraTextEditor2.Text = item.Filename;
                    }
                    if (item.ShowKind == (int)OwnerKinds.进攻行进)
                    {
                        ultraTextEditor1.Text = item.Filename;
                    }
                }

                if (item.ShowWhat == (int)ShowWhat.目标)
                {
                    if (item.ShowKind == (int)OwnerKinds.图片)
                    {
                        ultraComboEditor1.Items.Add(item.Filename).Tag = item.PositionScale;
                    }
                    if (item.ShowKind == (int)OwnerKinds.图片单向线型 || item.ShowKind == (int)OwnerKinds.图片双向线型)
                    {
                        ultraComboEditor2.Items.Add(item.Filename).Tag = item.pic;

                    }

                }

            }
            comKind.SelectedIndex = 0;
            this.ultraComboEditor1.SelectedIndex = 0;
            this.ultraComboEditor2.SelectedIndex = 0;


        }

        private void ClearForm()
        {
            comKind.Items.Clear();
            ultraComboEditor1.Items.Clear();
            ultraComboEditor2.Items.Clear();
            ultraTextEditor1.Text = "";
            ultraTextEditor2.Text = "";
            ultraTextEditor3.Text = "";
            ultraTextEditor1.ReadOnly = true;
            ultraTextEditor2.ReadOnly = true;
            ultraTextEditor3.ReadOnly = true;

            ultraNumericEditor1.Value = 1;
            ultraNumericEditor2.Value = 1;
            ultraNumericEditor3.Value = 1;
            ultraNumericEditor4.Value = 1;
            ultraNumericEditor5.Value = 1;
            ultraNumericEditor6.Value = 1;
            ultraNumericEditor7.Value = 1;
            ultraNumericEditor8.Value = 1;
            textBox1.Text = "";



        }


        private void button1_Click(object sender, EventArgs e)
        {
            DeleteComBox(comKind, OwnerKinds.图片);

        }

        private void DeleteComBox(Infragistics.Win.UltraWinEditors.UltraComboEditor comKind, OwnerKinds ownerkinds)
        {

            if (comKind.SelectedIndex < 0)
            {
                return;
            }
            p_armkinds.DeleteCurrentID(treeArmy.SelectedNodes[0].Key, comKind.SelectedItem.DisplayText, ownerkinds);
            comKind.Items.Remove(comKind.SelectedIndex);
            comKind.SelectedIndex = 0;
            modifyed = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DeleteComBox(ultraComboEditor1, OwnerKinds.图片);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DeleteComBox(ultraComboEditor2, OwnerKinds.图片);
        }

        private void comKind_ValueChanged(object sender, EventArgs e)
        {
            try
            {

                string tag = (string)comKind.SelectedItem.Tag;
                string[] array = Regex.Split(tag, ",");

                ultraNumericEditor2.Value = array[0];
                ultraNumericEditor3.Value = array[1];
                ultraNumericEditor5.Value = array[2];
                ultraNumericEditor1.Value = array[3];
                ultraNumericEditor10.Value = array[4];
                textBox1.Text = array[5];

            }
            catch (Exception ex)
            {

            }

        }

        private void ultraNumericEditor1_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                comKind.SelectedItem.Tag = ultraNumericEditor2.Value.ToString() + "," + ultraNumericEditor3.Value.ToString() + "," + ultraNumericEditor5.Value.ToString() + "," + ultraNumericEditor1.Value.ToString() + "," + ultraNumericEditor10.Value.ToString() + "," + textBox1.Text;
            }
            catch (Exception ex)
            {
            }

        }

        private void ultraNumericEditor4_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor1.SelectedItem.Tag = ultraNumericEditor8.Value.ToString() + "," + ultraNumericEditor7.Value.ToString() + "," + ultraNumericEditor6.Value.ToString() + "," + ultraNumericEditor4.Value.ToString() + ",," + textBox2.Text;
            }
            catch (Exception ex)
            {
            }

        }

        private void ultraNumericEditor9_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor2.SelectedItem.Tag = ultraNumericEditor9.Value.ToString() + "," + textBox3.Text;
            }
            catch (Exception ex)
            {
            }

        }

        private void ultraComboEditor1_ValueChanged(object sender, EventArgs e)
        {
            string tag = (string)ultraComboEditor1.SelectedItem.Tag;
            string[] array = Regex.Split(tag, ",");

            ultraNumericEditor8.Value = array[0];
            ultraNumericEditor7.Value = array[1];
            ultraNumericEditor6.Value = array[2];
            ultraNumericEditor4.Value = array[3];
          
                textBox2.Text = array[5];
        


        }

        private void ultraComboEditor2_ValueChanged(object sender, EventArgs e)
        {
            //p_armkinds.SelectPosition(treeArmy.SelectedNodes[0].Key, this.ultraComboEditor2.SelectedItem.DisplayText, OwnerKinds.图片);
            //try
            //{

            //    maskedTextBox1.Text = (string)ultraComboEditor2.SelectedItem.Tag;
            //}
            //catch (Exception ex)
            //{
            //}

            string  u2tag  = (string)ultraComboEditor2.SelectedItem.Tag;
            string[] array = Regex.Split(u2tag, ",");
            ultraNumericEditor9.Value = array[0];
            textBox3.Text = array[1];

        }

        private void ultraNumericEditor2_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                comKind.SelectedItem.Tag = ultraNumericEditor2.Value.ToString() + "," + ultraNumericEditor3.Value.ToString() + "," + ultraNumericEditor5.Value.ToString() + "," + ultraNumericEditor1.Value.ToString() + "," + ultraNumericEditor10.Value.ToString() + "," + textBox1.Text;
            }
            catch (Exception ex)
            {
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void ultraNumericEditor3_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                comKind.SelectedItem.Tag = ultraNumericEditor2.Value.ToString() + "," + ultraNumericEditor3.Value.ToString() + "," + ultraNumericEditor5.Value.ToString() + "," + ultraNumericEditor1.Value.ToString() + "," + ultraNumericEditor10.Value.ToString() + "," + textBox1.Text;
            }
            catch (Exception ex)
            {
            }
        }

        private void ultraNumericEditor5_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                comKind.SelectedItem.Tag = ultraNumericEditor2.Value.ToString() + "," + ultraNumericEditor3.Value.ToString() + "," + ultraNumericEditor5.Value.ToString() + "," + ultraNumericEditor1.Value.ToString() + "," + ultraNumericEditor10.Value.ToString() + "," + textBox1.Text;
            }
            catch (Exception ex)
            {
            }
        }

        private void ultraNumericEditor8_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor1.SelectedItem.Tag = ultraNumericEditor8.Value.ToString() + "," + ultraNumericEditor7.Value.ToString() + "," + ultraNumericEditor6.Value.ToString() + "," + ultraNumericEditor4.Value.ToString() + ",," + textBox2.Text;
            }
            catch (Exception ex)
            {
            }

        }

        private void ultraNumericEditor7_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor1.SelectedItem.Tag = ultraNumericEditor8.Value.ToString() + "," + ultraNumericEditor7.Value.ToString() + "," + ultraNumericEditor6.Value.ToString() + "," + ultraNumericEditor4.Value.ToString() + ",," + textBox2.Text;
            }
            catch (Exception ex)
            {
            }

        }

        private void ultraNumericEditor6_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor1.SelectedItem.Tag = ultraNumericEditor8.Value.ToString() + "," + ultraNumericEditor7.Value.ToString() + "," + ultraNumericEditor6.Value.ToString() + "," + ultraNumericEditor4.Value.ToString() + ",," + textBox2.Text;
            }
            catch (Exception ex)
            {
            }

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void ultraNumericEditor1_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor2_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor3_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor5_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor4_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor8_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor7_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor6_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void ultraNumericEditor9_MouseUp(object sender, MouseEventArgs e)
        {
            modifyed = true;
        }

        private void FrmShowKinds_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (modifyed)
            {

                if (MessageBox.Show("已修改，请保存！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    e.Cancel = true;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                comKind.SelectedItem.Tag = ultraNumericEditor2.Value.ToString() + "," + ultraNumericEditor3.Value.ToString() + "," + ultraNumericEditor5.Value.ToString() + "," + ultraNumericEditor1.Value.ToString() + "," + ultraNumericEditor10.Value.ToString() + "," + textBox1.Text;
            }
            catch (Exception ex)
            {
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor1.SelectedItem.Tag = ultraNumericEditor8.Value.ToString() + "," + ultraNumericEditor7.Value.ToString() + "," + ultraNumericEditor6.Value.ToString() + "," + ultraNumericEditor4.Value.ToString() + ",," + textBox2.Text;
            }
            catch (Exception ex)
            {
            }
        }

        private void ultraNumericEditor10_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                comKind.SelectedItem.Tag = ultraNumericEditor2.Value.ToString() + "," + ultraNumericEditor3.Value.ToString() + "," + ultraNumericEditor5.Value.ToString() + "," + ultraNumericEditor1.Value.ToString() + "," + ultraNumericEditor10.Value.ToString() + "," + textBox1.Text;
            }
            catch (Exception ex)
            {
            }
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            try
            {
                ultraComboEditor2.SelectedItem.Tag = ultraNumericEditor9.Value.ToString() + "," + textBox3.Text;
            }
            catch (System.Exception ex)
            {
            	
            }
           
        }


    }
}
